// pages/index1/index1.js

var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    loading: false,
    plain: false,
    disabled: false,
    sname:'',
    sfzh:'',
    wxid: '',
    scindex:0,
    schs: [],
    djson:{}
  },

  /**
   * 生命周期函数--监听页面加载 
   */
  onLoad: function () {
    //查询所有学校
    this.getAllSchool();
    var wxid = wx.getStorageSync('openid');
    this.setData({
      wxid: wxid
    })
  },

  //输入学生姓名
  snameinput: function (e) {
    this.setData({
      sname: e.detail.value
    })
  },
 
  //身份证号
  sfzhinput: function (e) {
    this.setData({
      sfzh: e.detail.value
    })
  },

  getAllSchool: function (e){
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/getAllSchool.html',//请求地址
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        var schs = [{CSID:'',CSNAME:'请选择学校'}];
        for (var i = 0; i < res.data.length; i++) {
          var sch = res.data[i];
          schs.push(sch);
        }
        that.setData({
          schs: schs
        });
      }
    })
  },


  bindPickerChange: function (e) {
    this.setData({
      scindex: e.detail.value
    })
  },

   //账号注册
  uaccountlogin: function (e) {
    wx.clearStorageSync();//去掉缓存
    wx.clearStorage();//去掉缓存
    var schs = this.data.schs;
    var scindex = this.data.scindex;
    var csid = schs[scindex].CSID;
    if (csid==''){
      wx.showModal({
        title: '友情提示',
        content: '请选择你所就读的学校!',
        showCancel: !1,
      })
      return;
    }
    var sname = this.data.sname;
    var sfzh = this.data.sfzh;
    if (sname == '') {
      wx.showModal({
        title: '友情提示',
        content: '请输入学生姓名!',
        showCancel: !1,
      })
      return;
    }
 
    if (sfzh == ''){
      wx.showModal({
        title: '友情提示',
        content: '请输入身份证号!',
        showCancel: !1,
      })
      return;
    }

    var djson={
      csid: csid,
      sname: sname,
      sfzh: sfzh
    };
    

    wx.showLoading({
      title: '注册中,请等待......',
    })

    var that = this;
    that.setData({
      disabled:true,
      loading: true
    })
    
    //1.先查看是否有该学生
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/findStudentExist.html',//请求地址
      data: djson,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        if (res.data.length==0){
          wx.hideLoading();
          wx.showModal({
            title: '友情提示',
            content: '对不起，该校没有你的资料，请联系你的老师!',
            showCancel: !1,
          })
          that.setData({
            disabled: false,
            loading: false
          })
        }else{
          var sname = res.data[0].SNAME;
          that.setData({
            djson: {
              csid: csid,
              sfzh: sfzh,
              sname: sname,
              openid: that.data.wxid,
              nj: res.data[0].NJ,
              bj: res.data[0].BJ,
            }
          })
          //2.然后在注册
          that.registStudent();
        }
      }
    })
  },

  //2.然后在注册
  registStudent: function (e) {  
    var djson = this.data.djson;
    var that = this;
    wx.request({
      url: app.myUlife.ulifeUrl + '/wechart/registStudent.html',//请求地址
      data: djson,
      header: {
        'content-type': 'application/json' // 默认值
      },
      success: function (res) {
        wx.setStorageSync('csid', djson.csid);//加入缓存
        wx.setStorageSync('sfzh', that.data.sfzh);//加入缓存
        wx.setStorageSync('sname', djson.sname);
        wx.setStorageSync('nj', djson.nj);
        wx.setStorageSync('bj', djson.bj);

        wx.hideLoading();
        //跳转到首页
        wx.switchTab({
          url: '../../pages/home/home'
        })
        
      }
    })
  }
  

})
